package com.ezops.poc.validations.validators.converter;

import java.util.regex.Pattern;

import org.antlr.grammar.v3.ANTLRv3Parser.notSet_return;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Utility class for checking type validation of decimal field.
 * <p>
 * Decimal value is validated apart from that the format decided the
 * type/constrainted defined for the decimal . The known formats as of now are
 * <p>
 * (*.2) - Upto two decimal place, (*.0) - No decimal value,
 * 
 * 
 * @author vgaurav
 * 
 */

public class EzopsDecimalConverter implements ITypeConverter {
	private static final Logger logger = LoggerFactory
			.getLogger(EzopsDecimalConverter.class);

	@Override
	public void tryConvert(String value, String format)
			throws ConversionException, UnknownFormatException {
		logger.trace("Converting value {} into format {}", value, format);
		try {

			// First check if it is a double value
			Double.valueOf(value);// No excption indicates that its a double
									// value

			// now check format if present
			if (format == null || format.equals("")) {
				return;
			}

			int maxNoOfAllowedDigit = Integer.valueOf(format.split("\\.")[1]);
			if (value.split("\\.")[1].length() > maxNoOfAllowedDigit) {
				throw new ConversionException("Decimal", value,
						"Decimal is more than allowed max size "
								+ maxNoOfAllowedDigit);
			}

		} catch (Exception e) {
			throw new ConversionException("Decimal", value, e.getMessage());
		}
	}

}
